package insight.google.game.era.data;


import insight.google.game.era.bo.GameRoom;
import insight.google.game.era.data.AppDataManagerFactory.DataManager;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class SimpleRoomDataManager extends AppDataManager {

	
	private static final Log log = LogFactory.getLog(RoomDataManager.class);
	
	
	public SimpleRoomDataManager(){
		this.isDetachable = true;
		super.initialize();
	}
	
	public SimpleRoomDataManager(TransactionHandler txHandler){
		this.txHandler = txHandler;
		super.initialize();
	}
	
	
	public SimpleRoomDataManager(TransactionHandler txHandler,boolean isDetachable) {
		this.txHandler = txHandler;
		this.isDetachable = isDetachable;
		super.initialize();
	}
	
//	@Override
//	public boolean addToDataStore(Object o) {
//		try {
//			this.dataStoreMgr.save(o,pManager);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return false;
//	}
	
	public HashMap<String, Room> initAllRoom(){
		HashMap<String, Room> roomListMap =  this.findAll();
		Room room = null;
		if(roomListMap.size()<=0){
			for (int i = 0; i < 10; i++) {
				room = new Room("Room # " + i);
				this.addToDataStore(room);
				GameRoom gameRoom = new GameRoom(room.getName());
				this.addToDataStore(gameRoom);
			}
			roomListMap = this.findAll();
		}
		return roomListMap;
	}
	
	public boolean addAllToDataStore(Collection<Room> o) {
		this.dataStoreMgr.saveAll(o,pManager);
		return false;
	}

	@Override
	public HashMap<String, Room> findAll() {
		List<Room> gameRoomList=null;
		try {
			gameRoomList = this.dataStoreMgr.get(Room.class,pManager,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Collections.sort(gameRoomList);
//		log.debug("Got All room from DadataStore  ---  "+gameRoomList.size());
		HashMap<String, Room> roomMap = new LinkedHashMap<String, Room>();
		if(gameRoomList != null){
			for (Room gameRoom : gameRoomList) {
				roomMap.put(gameRoom.getName(), gameRoom);
			}
		}
		return roomMap;
	}
	
	public Room findByName(String roomName) {
		StringBuffer cond = new StringBuffer();
		cond.append("name == '" + roomName + "'");
		return getSingleRoom(cond.toString());
	}
	
	public Room findById(String roomId) {
		StringBuffer cond = new StringBuffer();
		cond.append("roomId == '" + KeyFactory.stringToKey(roomId) + "'");
		return getSingleRoom(cond.toString());
		
	}
	
	public Room findById(Key roomId) {
		StringBuffer cond = new StringBuffer();
		cond.append("roomId == '" + KeyFactory.keyToString(roomId) + "'");
		return getSingleRoom(cond.toString());
		
	}

	
	public String findRoomNameById(Key roomId) {
		StringBuffer cond = new StringBuffer();
		cond.append("roomId == '" + roomId + "'");
		Room room = getSingleRoom(cond.toString());
		if(room != null){
			return room.getName();
		}
		return null;
	}
	
	private Room getSingleRoom(String cond){
		Room room = null;
		List<Room> userList= null;
		try {
			userList = DataStoreManager.getInstance().getByCondition(Room.class, cond,pManager,this.isDetachable);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (userList != null) {
			for (int i = 0; i < userList.size(); i++) {
				room = (Room)userList.get(i);
				break;
			}
		}
		return room;
	}
	
	


	

}
